//
// Created by 郭涧川 on 16/7/6.
//


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "include/locketdef.h"
#include "include/locketcipher.h"
#include "include/locketaes.h"
#include "include/locketerrno.h"
#include "include/lockethttpagent.h"

#include <jni.h>
#include <android/log.h>

void AES_Test (void)
{
    int iErr = ERR_COMMON_SUCCESS;

    /* ³õÊ¼»¯ */
    LOCKET_CIPHER_Init();

    /* AES ¼ÓÃÜ */
    CIPHER_AES_S stCipherAes;
    memset (&stCipherAes, 0, sizeof (stCipherAes));

    stCipherAes.pucInput = (unsigned char*)"hello,world";
    stCipherAes.iInputLen = strlen((char*) stCipherAes.pucInput);
    stCipherAes.iInputEncode = ENCODE_BINARY;

    unsigned char szbuffer[1024*2]= {0};
    stCipherAes.iMode = ENCRYPT;
    stCipherAes.iOutputEncode = ENCODE_BASE64;
    stCipherAes.pucOutput = szbuffer;

    stCipherAes.pucKey = (unsigned char*)"dGhpcyBpcyBrZXk=";
    stCipherAes.pucIv = (unsigned char*)"dGhpcyBpcyBpdg==";

    snprintf(stCipherAes.szAlgoName, sizeof (stCipherAes.szAlgoName), "%s", "AES-128-CFB");

    iErr = LOCKET_CIPHER_AESCrypt (&stCipherAes);
    if (ERR_COMMON_SUCCESS != iErr)
    {
        printf ("AES encrypt failed, iErr:%d\n", iErr);
                __android_log_print(ANDROID_LOG_ERROR,"locket","AES encrypt failed, iErr:%d\n", iErr);

        return;
    }
     __android_log_print(ANDROID_LOG_INFO,"Encrypt","%s", stCipherAes.pucOutput);

    /* AES ½âÃÜ */
    stCipherAes.iMode = DECRYPT;
    stCipherAes.pucInput = stCipherAes.pucOutput;
    stCipherAes.iInputLen = stCipherAes.iOutputLen;
    stCipherAes.iInputEncode = ENCODE_BASE64;

   // stCipherAes.pucKey = (unsigned char*)"this is key";
    //stCipherAes.pucIv = (unsigned char*)"this is iv";

    unsigned char szbuffer2[1024*2]= {0};
    stCipherAes.pucOutput = szbuffer2;
    stCipherAes.iOutputEncode = ENCODE_BINARY;
    iErr = LOCKET_CIPHER_AESCrypt (&stCipherAes);
    if (ERR_COMMON_SUCCESS != iErr)
    {
        __android_log_print(ANDROID_LOG_ERROR,"locket","AES decrypt failed");
        printf ("AES decrypt failed");
        return;
    }
     __android_log_print(ANDROID_LOG_INFO,"Decrypt","%s", stCipherAes.pucOutput);

    printf ("finish\n");
	return;
}
#if 0
void* thread_func (void *param)
{
    while (1)
    {
       __android_log_print(ANDROID_LOG_INFO,"locket","janky");
       sleep (1);
    }

    return NULL;
}

void createThread (void)
{
    pthread_t threadID = 0;
    if (0 != pthread_create (&threadID, NULL, thread_func, NULL))
    {
           __android_log_print(ANDROID_LOG_ERROR,"locket","create thread failed\n");

        return;
    }

    sleep (2);
    return;
}
#endif
JNIEXPORT void JNICALL Java_com_example_max_android_1test_MainActivity_aesTest
  (JNIEnv * env, jobject jo)
{

    AES_Test();

     __android_log_print(ANDROID_LOG_INFO,"da init 1","11ss");
    //int err = LOCKET_DA_Init (15558);
    int err = LOCKET_HTTPAGENT_Init ("www.sohu.com", 80);
     __android_log_print(ANDROID_LOG_INFO,"da init 2", "err:%d",err);

    //createThread ();

}